package edu.gznu.ics.fast.pulsar.service.userService.impl;

import edu.gznu.ics.fast.pulsar.dao.user.userRoleMapper;
import edu.gznu.ics.fast.pulsar.model.po.user.userRolePO;
import edu.gznu.ics.fast.pulsar.service.userService.ResourceService;
import edu.gznu.ics.fast.pulsar.service.userService.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>User: Zhang Kaitao
 * <p>Date: 14-1-28
 * <p>Version: 1.0
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {

    @Autowired
    private userRoleMapper userRoleMapper;
    @Autowired
    private ResourceService resourceService;

    public userRolePO createRole(userRolePO role) {
        return userRoleMapper.insertRole(role);
    }

    public userRolePO updateRole(userRolePO role) {
        return userRoleMapper.updateRole(role);
    }

    public void deleteRole(Long roleId) {
        userRoleMapper.deleteRoleByRoleId(roleId);
    }

    @Override
    public userRolePO findRoleInfoByRoleId(Long roleId) {
        return userRoleMapper.queryRoleByRoleId(roleId);
    }

    @Override
    public List<userRolePO> findAllRoles() {
        return userRoleMapper.queryAllRoles();
    }


    @Override
    public Set<String> findRoles(Long[] roleIds) {
        Set<String> roles = new HashSet<String>();
        for (Long roleId : roleIds) {
            userRolePO role = findRoleInfoByRoleId(roleId);
            if (role != null) {
                roles.add(role.getRole());
            }
        }
        return roles;
    }

    @Override
    public Set<String> findPermissions(Long[] roleIds) {
        Set<Long> resourceIds = new HashSet<Long>();
        for (Long roleId : roleIds) {
            userRolePO role = findRoleInfoByRoleId(roleId);
            if (role != null) {
                // resourceIds.addAll(role.getResourceIds());
            }
        }
        return resourceService.findPermissions(resourceIds);
    }
}
